<?php


namespace app\api\model;


use app\common\library\IDMan;
use app\common\library\PlatformModel;
use app\common\library\StatModel;
use app\common\model\BaseModel;
use common;

class UserRideCardOrderModel extends BaseModel
{
    protected $name = 'user_ridecard_order';
    protected $pk = 'id';

    var $ride_cards =[
        'default' =>  [
            [
                'id' => 1,                   //id
                'name' => '5次·7日骑行卡',    //名字
                'price' => 900,            //非会员价
                'old_price' => 3000,        //非会员价划线
                'yprice' => 800,            //会员价
                'old_yprice' => 1000,        //非会员价划线
                'mem_duration'=> 7*24*3600, //赠送的会员时间
                'ride_duration' => 7*24*3600,   //骑行卡有效时间
                'free_count' => 5,              //次数
            ],
            [
                'id' => 2,
                'name' => '10次·15日骑行卡',
                'price' => 1590, //690
                'old_price'=> 6000,
                'yprice' => 1700,
                'old_yprice' => 2400,
                'mem_duration'=> 15*24*3600,
                'ride_duration' => 15*24*3600,
                'free_count' => 10,              //次数
            ],
            [
                'id' => 3,
                'name' => '30次·30日骑行卡',
                'price' => 4590, //690
                'old_price'=> 18000,
                'yprice' => 3500,
                'old_yprice' => 6000,
                'mem_duration'=> 30*24*3600,
                'ride_duration' => 30*24*3600,
                'free_count' => 30,              //次数
            ],
        ],

        '350800' => [
            [
                'id' => 1,                   //id
                'name' => '5次·7日骑行卡',    //名字
                'price' => 888,            //非会员价
                'old_price' => 1500,        //非会员价划线
                'yprice' => 800,            //会员价
                'old_yprice' => 1000,        //非会员价划线
                'mem_duration'=> 7*24*3600, //赠送的会员时间
                'ride_duration' => 7*24*3600,   //骑行卡有效时间
                'free_count' => 5,              //次数
            ],
            [
                'id' => 2,
                'name' => '10次·15日骑行卡',
                'price' => 1288, //690
                'old_price'=> 3000,
                'yprice' => 1700,
                'old_yprice' => 2400,
                'mem_duration'=> 15*24*3600,
                'ride_duration' => 15*24*3600,
                'free_count' => 10,              //次数
            ],
            [
                'id' => 3,
                'name' => '30次·30日骑行卡',
                'price' => 2888, //690
                'old_price'=> 9000,
                'yprice' => 3500,
                'old_yprice' => 6000,
                'mem_duration'=> 30*24*3600,
                'ride_duration' => 30*24*3600,
                'free_count' => 30,              //次数
            ],
        ],
        '451100' => [
            [
                'id' => 1,                   //id
                'name' => '5次·7日骑行卡',    //名字
                'price' => 588,            //非会员价
                'old_price' => 1500,        //非会员价划线
                'yprice' => 800,            //会员价
                'old_yprice' => 1000,        //非会员价划线
                'mem_duration'=> 7*24*3600, //赠送的会员时间
                'ride_duration' => 7*24*3600,   //骑行卡有效时间
                'free_count' => 5,              //次数
            ],
            [
                'id' => 2,
                'name' => '10次·15日骑行卡',
                'price' => 988, //690
                'old_price'=> 3000,
                'yprice' => 1700,
                'old_yprice' => 2400,
                'mem_duration'=> 15*24*3600,
                'ride_duration' => 15*24*3600,
                'free_count' => 10,              //次数
            ],
            [
                'id' => 3,
                'name' => '30次·30日骑行卡',
                'price' => 2088, //690
                'old_price'=> 9000,
                'yprice' => 3500,
                'old_yprice' => 6000,
                'mem_duration'=> 30*24*3600,
                'ride_duration' => 30*24*3600,
                'free_count' => 30,              //次数
            ],
        ],
        '450924' => [
            [
                'id' => 1,                   //id
                'name' => '5次·7日骑行卡',    //名字
                'price' => 599,            //非会员价
                'old_price' => 1000,        //非会员价划线
                'yprice' => 800,            //会员价
                'old_yprice' => 1000,        //非会员价划线
                'mem_duration'=> 7*24*3600, //赠送的会员时间
                'ride_duration' => 7*24*3600,   //骑行卡有效时间
                'free_count' => 5,              //次数
            ],
            [
                'id' => 2,
                'name' => '10次·15日骑行卡',
                'price' => 999, //690
                'old_price'=> 2000,
                'yprice' => 1700,
                'old_yprice' => 2400,
                'mem_duration'=> 15*24*3600,
                'ride_duration' => 15*24*3600,
                'free_count' => 10,              //次数
            ],
            [
                'id' => 3,
                'name' => '30次·30日骑行卡',
                'price' => 2699, //690
                'old_price'=> 6000,
                'yprice' => 3500,
                'old_yprice' => 6000,
                'mem_duration'=> 30*24*3600,
                'ride_duration' => 30*24*3600,
                'free_count' => 30,              //次数
            ],
        ],
    ];




    function __construct()
    {
    }

    function card_list($city_id)
    {
        if (empty($city_id)){
            return $this->ride_cards['default'];
        }

        if (empty($this->ride_cards[$city_id])){
            return $this->ride_cards['default'];
        }

        return $this->ride_cards[$city_id];
    }

    function query_card_info($city_id,$id)
    {
        $card_list = $this->card_list($city_id);

        foreach ($card_list as $info) {
            if ($info['id'] == $id) {
                return $info;
            }
        }
        return null;
    }

    function submit_wxmp($city_id, $userinfo, $card_id)
    {
        $card_info = $this->query_card_info($city_id,$card_id);
        if (empty($card_info)) {
            $this->throw_myexception(X_ERROR, "查询骑行卡失败!");
        }

        $pay_city = 'default';
        if (empty($pay_city)){
            $pay_city = 'default';
        }

        $order_id = IDMan::order_id();

        $price = $card_info['price'];
//        if (!empty($userinfo)) {
//            if (isset($userinfo['membership_expire_time'])) {
//                if (!empty($userinfo['membership_expire_time'])) {
//                    $price =$card_info['yprice'];
//                }
//            }
//        }

        $ret = (new PayModel($pay_city))->wxmp_unifield_order($city_id, ORDER_TYPE_USER_RIDECARD, $order_id, $price, $card_info['name'],$pay_city);
        if (empty($ret)) {
            $this->throw_myexception(X_ERROR, "统一下单失败!");
        }

        $platform = (new PlatformModel())->get_platform_info('wxmpu');


        $prepay_info = [
            'appId' => $platform['appid'],
            'package' => 'prepay_id=' . $ret['prepay_id'],
            'nonceStr' => uniqid(),
            'timeStamp' => time(),
            'signType' => 'MD5',
        ];

        $sign = (new PayModel($city_id))->wx_sign($prepay_info);
        $prepay_info['sign'] = $sign;
        //$prepay_info['mch_id'] = $ret['mch_id'];
        $order_info = [
            'id' => $order_id,
            'city_id' => $city_id,
            'amount' => $price,
            'user_id' => $userinfo['id'],
            'user_name' => $userinfo['name'],
            'user_phone' => $userinfo['phone'],
            'create_time' => date('Y-m-d H:i:s'),
            'order_state' => USER_RIDECARD_ORDER_STATE_SUBMIT,
            'gateway' => PAY_GATEWAY_WEIXIN,
            'ride_duration' => $card_info['ride_duration'],
            'member_duration' => $card_info['mem_duration'],
            'ridecard_id' => $card_info['id'],
            'ridecard_name' => $card_info['name'],
            'free_count' => $card_info['free_count'],
            'free_count_total' => $card_info['free_count'],
            'pay_city' => $pay_city,
        ];

        self::create($order_info);
        return $prepay_info;
    }

    function submit_alipay($city_id, $userinfo, $card_id)
    {
        $card_info = $this->query_card_info($city_id,$card_id);
        if (empty($card_info)) {
            $this->throw_myexception(X_ERROR, "查询骑行卡失败!");
        }

        $alipay_user_id = $userinfo['alipay_user_id'];

        $price = $card_info['price'];
//        if (!empty($userinfo)) {
//            if (isset($userinfo['membership_expire_time'])) {
//                if (!empty($userinfo['membership_expire_time'])) {
//                    $price =$card_info['yprice'];
//                }
//            }
//        }

        $pay_city = 'default';
        if (empty($pay_city)){
            $pay_city = 'default';
        }
        $order_id = IDMan::order_id();
        $ret = (new PayModel($pay_city))->alipay_create_trade($city_id, ORDER_TYPE_USER_RIDECARD, $order_id, $alipay_user_id, $price, $card_info['name'],$pay_city);
        if (empty($ret)) {
            $this->throw_myexception(X_ERROR, "统一下单失败!");
        }

        $order_info = [
            'id' => $order_id,
            'city_id' => $city_id,
            'amount' => $price,
            'user_id' => $userinfo['id'],
            'user_name' => $userinfo['name'],
            'user_phone' => $userinfo['phone'],
            'create_time' => date('Y-m-d H:i:s'),
            'order_state' => USER_RIDECARD_ORDER_STATE_SUBMIT,
            'gateway' => PAY_GATEWAY_ALIPAY,
            'ride_duration' => $card_info['ride_duration'],
            'member_duration' => $card_info['mem_duration'],
            'ridecard_id' => $card_info['id'],
            'ridecard_name' => $card_info['name'],
            'free_count' => $card_info['free_count'],
            'free_count_total' => $card_info['free_count'],
            'pay_city' => $pay_city,
        ];

        self::create( $order_info);

        return $ret;
    }

    function submit_wxapp($city_id, $userinfo, $card_id)
    {
        $card_info = $this->query_card_info($city_id,$card_id);
        if (empty($card_info)) {
            $this->throw_myexception(X_ERROR, "查询骑行卡失败!");
        }

        $pay_city = 'default';
        if (empty($pay_city)){
            $pay_city = 'default';
        }

        $order_id = IDMan::order_id();

        $price = $card_info['price'];
//        if (!empty($userinfo)) {
//            if (isset($userinfo['membership_expire_time'])) {
//                if (!empty($userinfo['membership_expire_time'])) {
//                    $price =$card_info['yprice'];
//                }
//            }
//        }

        $ret = (new PayModel($pay_city))->wxapp_unifield_order($city_id, ORDER_TYPE_USER_RIDECARD, $order_id, $price, $card_info['name'],$pay_city);
        if (empty($ret)) {
            $this->throw_myexception(X_ERROR, "统一下单失败!");
        }

        $platform = (new PlatformModel())->get_platform_info('wxapp');


        $prepay_info = [
            'appid' => $platform['appid'],
            'package' => 'prepay_id=' . $ret['prepayid'],
            'noncestr' => uniqid(),
            'timestamp' => time(),
            'prepayid' => $ret['prepayid'],
            'partnerid' => $ret['partnerid'],
        ];


        $sign = (new PayModel($pay_city))->wx_sign($prepay_info);
        $prepay_info['sign'] = $sign;
        $prepay_info['mch_id'] = $ret['mch_id'];
        $order_info = [
            'id' => $order_id,
            'city_id' => $city_id,
            'amount' => $price,
            'user_id' => $userinfo['id'],
            'user_name' => $userinfo['name'],
            'user_phone' => $userinfo['phone'],
            'create_time' => date('Y-m-d H:i:s'),
            'order_state' => USER_RIDECARD_ORDER_STATE_SUBMIT,
            'gateway' => PAY_GATEWAY_WEIXIN,
            'ride_duration' => $card_info['ride_duration'],
            'member_duration' => $card_info['mem_duration'],
            'ridecard_id' => $card_info['id'],
            'ridecard_name' => $card_info['name'],
            'free_count' => $card_info['free_count'],
            'free_count_total' => $card_info['free_count'],
            'pay_city' => $pay_city,
        ];

        self::create($order_info);
        return $prepay_info;
    }

    function submit_aliapp($city_id, $userinfo, $card_id)
    {
        $card_info = $this->query_card_info($city_id,$card_id);
        if (empty($card_info)) {
            $this->throw_myexception(X_ERROR, "查询骑行卡失败!");
        }

        $alipay_user_id = $userinfo['alipay_user_id'];

        $price = $card_info['price'];
//        if (!empty($userinfo)) {
//            if (isset($userinfo['membership_expire_time'])) {
//                if (!empty($userinfo['membership_expire_time'])) {
//                    $price =$card_info['yprice'];
//                }
//            }
//        }

        $pay_city = 'default';
        if (empty($pay_city)){
            $pay_city = 'default';
        }
        $order_id = IDMan::order_id();
        $ret = (new PayModel($pay_city))->alipay_create_trade_app($city_id, ORDER_TYPE_USER_RIDECARD, $order_id, $price, $card_info['name'],$pay_city);
        if (empty($ret)) {
            $this->throw_myexception(X_ERROR, "统一下单失败!");
        }

        $order_info = [
            'id' => $order_id,
            'city_id' => $city_id,
            'amount' => $price,
            'user_id' => $userinfo['id'],
            'user_name' => $userinfo['name'],
            'user_phone' => $userinfo['phone'],
            'create_time' => date('Y-m-d H:i:s'),
            'order_state' => USER_RIDECARD_ORDER_STATE_SUBMIT,
            'gateway' => PAY_GATEWAY_ALIPAY,
            'ride_duration' => $card_info['ride_duration'],
            'member_duration' => $card_info['mem_duration'],
            'ridecard_id' => $card_info['id'],
            'ridecard_name' => $card_info['name'],
            'free_count' => $card_info['free_count'],
            'free_count_total' => $card_info['free_count'],
            'pay_city' => $pay_city,
        ];

        self::create($order_info);

        return $ret;
    }

    function finish($order_id, $payment_info)
    {
        $order_info = $this->query_info($order_id);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败!");
        }

        if ($order_info['order_state'] != USER_RIDECARD_ORDER_STATE_SUBMIT) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "订单状态不匹配!");
        }

        $userinfo = (new UserModel())->query_info($order_info['user_id']);

        //设置会员卡时间
//        $membership_start_time = null;
//        $membership_expire_time = null;
//        if (empty($userinfo['membership_expire_time']) || $userinfo['membership_expire_time'] < date('Y-m-d H:i:s')) {
//            $membership_start_time = date('Y-m-d H:i:s');
//            $membership_expire_time = date('Y-m-d H:i:s', time() + $order_info['member_duration']);
//        } else {
//            $membership_start_time = $userinfo['membership_expire_time'];
//            $membership_expire_time = date('Y-m-d H:i:s', strtotime($userinfo['membership_expire_time']) + $order_info['member_duration']);
//        }
//        (new UserModel())->set_membership_expire_time($order_info['user_id'], $membership_expire_time);

        //设置骑行卡时间
        $ride_start_time = null;
        $ride_expire_time = null;

//        if (empty($userinfo['ridecard_expire_time']) || $userinfo['ridecard_expire_time'] < date('Y-m-d H:i:s')) {
//            $ride_start_time = date('Y-m-d H:i:s');
//            $ride_expire_time = date('Y-m-d H:i:s', time() + $order_info['ride_duration']);
//        } else {
//            $ride_start_time = $userinfo['ridecard_expire_time'];
//            $ride_expire_time = date('Y-m-d H:i:s', strtotime($userinfo['ridecard_expire_time']) + $order_info['ride_duration']);
//        }
        $ride_start_time = date('Y-m-d H:i:s');
        $ride_expire_time = date('Y-m-d H:i:s', time() + $order_info['ride_duration']);
        (new UserModel())->set_ridecard_expire_time($order_info['user_id'], $ride_expire_time,$order_info['free_count_total']);

        $membership_id = IDMan::order_id();
        $data = [
            'order_state' => USER_RIDECARD_ORDER_STATE_FINISHED,
            'end_time' => date('Y-m-d H:i:s'),
            'payment_info' => $payment_info,
            'ridecard_start_time' => $ride_start_time,
            'ridecard_end_time' => $ride_expire_time,
//            'membership_order_id' =>$membership_id,
        ];
        $where = [
            'id' => $order_id,
        ];
        self::update($data, $where);


//        //添加membership
//        $order_info = [
//            'id' =>$membership_id,
//            'city_id' => $order_info['city_id'],
//            'amount' => 0,
//            'user_id' => $userinfo['id'],
//            'user_name' => $userinfo['name'],
//            'user_phone' => $userinfo['phone'],
//            'create_time' => date('Y-m-d H:i:s'),
//            'order_state' => USER_MEMBERSHIP_ORDER_STATE_FINISHED,
//            'gateway' => PAY_GATEWAY_WEIXIN,
//            'duration' => $order_info['member_duration'],
//            'membership_card_id' => 0,
//            'membership_card_name' => '骑行卡赠送',
//            'end_time' => date('Y-m-d H:i:s'),
//            'payment_info' => $payment_info,
////            'membership_start_time' => $membership_start_time,
////            'membership_end_time' => $membership_expire_time,
//            'ridecard_order_id' => $order_id,
//        ];
//
//        hisql::insert('tb_user_membership_order', $order_info);

        (new StatModel())->user_ridecard_order_amount_incr($order_info['city_id'], $order_info['amount']);
        (new StatNewModel())->rcard_incr($order_info['city_id'], $order_info['amount'], date('Y-m-d'));
    }

    function query_info($order_id)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'user_id' => 'string',
            'user_name' => 'string',
            'user_phone' => 'string',
            'gateway' => 'string',
            'amount' => 'int',
            'order_state' => 'int',
            'create_time' => 'string',
            'ride_duration' => 'int',
            'ridecard_start_time' => 'string',
            'ridecard_end_time' => 'string',
            'ridecard_id' => 'int',
            'ridecard_name' => 'string',
            'refund_order_id' => 'string',
            'refund_amount' => 'int',
            'membership_order_id' => 'string',
            'member_duration' => 'int',
            'free_count' => 'int',
            'free_count_total' => 'int',
        ];

        $sfs = common::array_keys($selectlist);
        $beans = [
            'id'=>$order_id,
        ];

        $info = $this->where($beans)->field($sfs)->find();
        return $info;
    }

    function query_my_list($cd, $pos, $limit)
    {
        $selectlist = [
            'id' => 'string',                       //骑行卡id
            'user_id' => 'string',                  //玩家id
            'user_name' => 'string',                //玩家名字
            'user_phone' => 'string',               //玩家手机号
            'gateway' => 'string',                  //支付途径
            'amount' => 'int',                      //订单金额
            'order_state' => 'int',                 //支付状态
            'create_time' => 'string',              //创建时间
            // 'payment_info' => 'array',
            'end_time' => 'string',                 //支付结束时间
            'ridecard_start_time' => 'string',      //骑行卡开始时间
            'ridecard_end_time' => 'string',        //骑行卡结束时间
            'ridecard_id' => 'int',                 //骑行卡时间
            'ridecard_name' => 'string',            //骑行卡名字
            'refund_order_id' => 'string',          //退款id
            'refund_amount' => 'int',               //退款金额
            'membership_order_id' => 'string',      //暂时无用
            'free_count' => 'int',                  //剩余次数
            'free_count_total' => 'int',            //总次数
        ];

        $sfs = common::array_keys($selectlist);
        $datenow = date('Y-m-d H:i:s');
        $beans = [
            'order_state' => USER_RIDECARD_ORDER_STATE_FINISHED
        ];
        if (isset($cd['user_id'])) {
            $beans['user_id'] = $cd['user_id'];
        }

        if (isset($cd['city_id'])) {
            $beans['city_id'] = $cd['city_id'];
        }


        if (isset($cd['is_use'])){
            $beans['ridecard_end_time'] = ['>',$datenow];
        }

        $rows =$this->where($beans)->field($sfs)->order('create_time','desc')->limit($pos,$limit)->select();
        return $rows->toArray();
    }

    function query_list($city_id, $cd, $pos, $limit)
    {
        $limit = min(30, $limit);
        $selectlist = [
            'id' => 'string',
            'user_id' => 'string',
            'user_name' => 'string',
            'user_phone' => 'string',
            'gateway' => 'string',
            'amount' => 'int',
            'order_state' => 'int',
            'create_time' => 'string',
            // 'payment_info' => 'array',
            'end_time' => 'string',
            'ridecard_start_time' => 'string',
            'ridecard_end_time' => 'string',
            'ridecard_id' => 'int',
            'ridecard_name' => 'string',
            'refund_order_id' => 'string',
            'refund_amount' => 'int',
            'membership_order_id' => 'string',
            'free_count' => 'int',
            'free_count_total' => 'int',
        ];

        $sfs = common::array_keys($selectlist);
        $beans = [
        ];
        if (!empty($city_id)) {
            $beans['city_id'] = $city_id;
        }

        if (isset($cd['user_id'])) {;
            $beans['user_id'] = $cd['user_id'];
        }

        $beans['order_state'] =['>=',USER_RIDECARD_ORDER_STATE_FINISHED];

        $rows =$this->where($beans)->field($sfs)->order('create_time','desc')->limit($pos,$limit)->select();
        return $rows->toArray();
    }

    function summit_refund($order_id, $amount, $reason)
    {
        $order_info = $this->query_info($order_id);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单信息失败！");
        }

        if ($order_info['order_state'] != USER_RIDECARD_ORDER_STATE_FINISHED) {
            $this->throw_myexception(ERROR_QUERY_INFO, "未完成支付订单！");
        }


        $ret = (new PayModel($order_info['city_id']))->submit_refund($order_id, ORDER_TYPE_USER_RIDECARD_REFUND, $amount, $reason);
        if ($ret) {
            $data = [
                'order_state' => USER_RIDECARD_ORDER_STATE_REFUND,
                'refund_order_id' => $ret->order_id,
                'refund_amount' => $amount,
            ];

            if ($ret->order_state == PAY_ORDER_STATE_REFUND_FINISH) {
                $data['payment_info'] = $ret;
                $data['end_time'] = date('Y-m-d H:i:s');
                $data['order_state'] = USER_RIDECARD_ORDER_STATE_REFUND_FINISHED;
            }
            $where = [
                'id' => $order_id,
            ];

            self::update($data, $where);

            $userinfo = (new UserModel())->query_info($order_info['user_id']);

            //先退骑行卡时间
            $expire_time = $userinfo['ridecard_expire_time'];
            if (!empty($expire_time)) {
                $expire_time = date('Y-m-d H:i:s', strtotime($expire_time) - $order_info['ride_duration']);
            }
            if ($expire_time <= date('Y-m-d H:i:s')) {
                $expire_time = null;
            }

            (new UserModel())->set_ridecard_expire_time($userinfo['id'], $expire_time,0);
        }

    }

    function refund_finish($order_id, $payment_info)
    {
        $order_info = $this->query_info($order_id);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败!");
        }

        if ($order_info['order_state'] != USER_RIDECARD_ORDER_STATE_REFUND) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "订单状态不匹配!");
        }

        $data = [
            'order_state' => USER_RIDECARD_ORDER_STATE_REFUND_FINISHED,
            'end_time' => date('Y-m-d H:i:s'),
        ];
        if ($payment_info) {
            $data['payment_info'] = $payment_info;
        }

        $where = [
            'id' => $order_id,
        ];

        self::update($data, $where);
    }

    function perform($user_id,$city_id){

        $cd = [
            'user_id' => $user_id,
            'order_state' => USER_RIDECARD_ORDER_STATE_FINISHED,
            'is_use' => 1,
            'city_id'=> $city_id,
        ];

        $list = $this->query_my_list($cd, 0, 20);

        foreach ($list as $val){
            if ($val['free_count']>0){

                $data = [
                    'free_count' => $val['free_count']-1,
                ];

                $where = [
                    'id' => $val['id'],
                ];

                if($val['free_count'] == 1){
                    (new UserModel())->set_ridecard_expire_time($user_id, null,$val['free_count']-1);
                }
                else {
                    (new UserModel())->set_ridecard_expire_time($user_id, $val['ridecard_end_time'],$val['free_count']-1);
                }


                self::update($data, $where);

                return $val['id'];
            }
        }

        return null;
    }

    function add_ridecard_time($userinfo,$order_id, $days,$count){

        $rideduration = $days * 24 * 3600;
        //
        $expire_time = $userinfo['ridecard_expire_time'];
        if (!empty($expire_time)) {
            $expire_time = date('Y-m-d H:i:s', strtotime($expire_time) + $rideduration);


            $data = [
                'ridecard_end_time' => $expire_time,
            ];

            $where = [
                'id' => $order_id,
            ];

            self::update($data, $where);

            (new UserModel())->set_ridecard_expire_time($userinfo['id'], $expire_time,$count);
        }
    }

    function add_ridecard($city_id,$userinfo,$gate_way,$name,$duration,$free_count)
    {
        $order_id = IDMan::order_id();

        $ride_start_time = date('Y-m-d H:i:s');
        $ride_expire_time = date('Y-m-d H:i:s', time() + $duration);

        $order_info = [
            'id' => $order_id,
            'city_id' => $city_id,
            'amount' => 0,
            'user_id' => $userinfo['id'],
            'user_name' => $userinfo['name'],
            'user_phone' => $userinfo['phone'],
            'create_time' => date('Y-m-d H:i:s'),
            'end_time' => date('Y-m-d H:i:s'),
            'order_state' => USER_RIDECARD_ORDER_STATE_FINISHED,
            'gateway' => $gate_way,
            'ride_duration' => $duration,
            'member_duration' => 0,
            'ridecard_id' => 99,
            'ridecard_name' => $name,
            'free_count' => $free_count,
            'free_count_total' => $free_count,
            'pay_city' => $city_id,
            'ridecard_start_time' => $ride_start_time,
            'ridecard_end_time' => $ride_expire_time,
        ];

        self::create($order_info);
        (new UserModel())->set_ridecard_expire_time($order_info['user_id'], $ride_expire_time);
    }
}
